﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code;
using Web.Framework;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion.App
{
    public partial class DestinoList : BasePage
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);
        }

        protected override void ApplySecurity()
        {
            this.btnAgregarNuevo.Visible = HasPermissionFor(PlanProdSecurity.DESTINO_CREAR);

            if (this.GridDestinos.DataSource != null)
            {
                this.GridDestinos.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_EDITAR);
                this.GridDestinos.Columns[1].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_ELIMINAR);
            }
        }

        private void Buscar()
        {
            string nombreLike = this.txtNombre.Text;

            List<Destino> destinos = WebServiceHelper.GetDestinos(BaseMasterPage.CompanyId, nombreLike);

            this.GridDestinos.DataSource = destinos;
            this.GridDestinos.DataBind();

            pnlResults.Visible = true;
        }

        protected void GridDestinos_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[0].Attributes.Add("title", "Editar");
                    e.Row.Cells[1].Attributes.Add("title", "Eliminar");
                    ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                    btnEliminarCtrl.OnClientClick = string.Format("return confirm('Está seguro que desea eliminar el siguiente registro? {0}')", ((Destino) e.Row.DataItem).Nombre);                    
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridDestinos_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int destinoId = Convert.ToInt32(this.GridDestinos.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format("/App/DestinoEdit.aspx?Id={0}", destinoId), true);
                }
                else if (e.CommandName == "eliminar")
                {
                    int destinoId = Convert.ToInt32(this.GridDestinos.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarDestino(destinoId);
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridDestinos_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.GridDestinos.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnAgregarNuevo_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("/App/DestinoEdit.aspx", true);
        }
    }
}